﻿@{
    ViewData["Title"] = "用户组管理";
}
@model Mango.Manager.ViewModels.ManagerPageViewModel
@section Style{
    <link href="~/lib/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
}
<table class="table">
    <tbody>
        <tr>
            <td>
                <input type="button" class="btn btn-success" id="btn_Add" value="添加用户组" />
            </td>
        </tr>
    </tbody>
</table>
<table class="table  table-sm table-hover table-list table-bordered">
    <thead>
        <tr>
            <th>Id</th>
            <th>用户组名称</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var row in Model.ListData)
        {
            <tr>
                <td>
                    @row["GroupId"]
                </td>
                <td>
                    @row["GroupName"]
                </td>
                <td>
                    <a href="javascript:EditInfo(@row["GroupId"],'@row["GroupName"]');" class="del">编辑</a>
                    <a href="javascript:SetPower(@row["GroupId"]) ;" class="del">设置权限</a>
                </td>
            </tr>
        }
    </tbody>
</table>
@*<div class="row">
    <div class="col-12">
        <ul class="pagination  justify-content-center">
            @Html.MPager(ViewContext.HttpContext.Request, Model.TotalCount)
        </ul>
    </div>
</div>*@

<script src="~/lib/ztree/js/jquery.ztree.core.min.js"></script>
<script src="~/lib/ztree/js/jquery.ztree.excheck.min.js"></script>

<script type="text/javascript">
    function EditInfo(groupId,groupName) { 
        layer.prompt({ formType: 0, value: groupName, title: '编辑用户组', },
            function (value, index, elem) {
                $.ajax({
                    type: 'post',
                    url: '/User/EditGroup',
                    data: 'GroupName=' + value + '&GroupId=' + groupId,
                    success: function (res) {
                        if (res) {
                            alert('修改成功');
                            window.location.href = window.location.href;
                        }
                    }
                });
            }
        );
    }
    $("#btn_Add").click(function () {
        layer.prompt({ title: '添加用户组', formType: 0 }, function (text, index) {
            //layer.close(index);
            $.ajax({
                type: 'post',
                url: '/User/AddGroup',
                data: 'GroupName=' + text,
                success: function (res) {
                    if (res) {
                        alert('添加成功');
                        window.location.href = window.location.href;
                    }
                }
            });
        });
    });

</script>
<script type="text/javascript">
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };

    var zNodes = new Array();

    var code;

    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("showTree");
        zTree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
    }

    $(document).ready(function () {
        //加载菜单信息
        $.ajax({
            type: 'get',
            url: '/User/GetMenuByAll',
            success: function (result) {
                var json = result;
                console.log(json);
                $.each(json, function (i) {
                    zNodes.push({
                        id: json[i].data.MId, pId: json[i].data.ParentId, name: json[i].data.MName, open: true, checked: false
                    });
                });
                console.log(zNodes);
            }
        });
    });
    //权限设置
    function SetPower(groupId) {
        var html = '<div id="showTree" class="ztree"></div>';
        var layIndex = layer.open({
            type: 1,
            title: '设置权限:',
            shadeClose: true,
            content: html,
            area: ['300px', '400px'],
            btn: ['保 存'],
            btn1: function () {
                var zTree = $.fn.zTree.getZTreeObj("showTree");
                var sc = zTree.getCheckedNodes();
                var mid = new Array();
                for (var i = 0; i < sc.length; i++) {
                    if (sc[i].checked) {
                        mid.push(sc[i].id);
                    }
                }
                $.ajax({
                    type: 'post',
                    url: '/User/SetGroupPower',
                    data: { 'groupId': groupId, 'powerData': mid },
                    success: function (res) {

                        if (res) {
                            //信息框-例2
                            layer.msg('保存成功!', {
                                time: 2000 //不自动关闭
                            });
                            setTimeout(function () {
                                layer.close(layIndex);
                                window.location.href = window.location.href;
                            }, 2000);

                        }
                    }
                });
            }
        });
        //清空原有选择
        for (var j = 0; j < zNodes.length; j++) {
            zNodes[j].checked = false;
        }
        //加载现有权限信息
        $.ajax({
            type: 'get',
            url: '/User/GetGroupPower',
            data: 'groupId=' + groupId,
            success: function (result) {
                var json = result;
                console.log(json);
                var zTree = $.fn.zTree.getZTreeObj("showTree");
                for (var j = 0; j < zNodes.length; j++) {
                    $.each(json, function (i) {
                        if (json[i].mId == zNodes[j].id) {
                            zNodes[j].checked = true;
                        }
                    });
                }
            },
            async: false
        });
        $.fn.zTree.init($("#showTree"), setting, zNodes);
        setCheck();

    }
</script>